package com.dragon.avltree;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/5/22 9:53
 */
public class AVLTree<K extends Comparable<K>, V> implements Map<K, V> {

    private Node root;

    private int size;

    private class Node {
        K key;
        V value;
        Node left;
        Node right;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            left = null;
            right = null;
        }
    }

    public AVLTree() {
        this.root = null;
        this.size = 0;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }

    @Override
    public boolean containsKey(Object key) {
        return false;
    }

    public boolean containKey(K key) {
        return getNode(root, key) != null;
    }

    @Override
    public boolean containsValue(Object value) {
        return false;
    }

    @Override
    public V get(Object key) {
        return null;
    }

    public V get(K key) {
        Node resultNode = getNode(root, key);
        return resultNode != null ? resultNode.value : null;
    }

    private Node getNode(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (node.key.compareTo(key) > 0) {
            return getNode(node.left, key);
        } else if (node.key.compareTo(key) < 0) {
            return getNode(node.right, key);
        } else {
            return node;
        }
    }

    /**
     * 返回以node为根的二分搜索树的最小值所在的节点
     *
     * @param node
     * @return
     */
    private Node minimum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minimum(node.left);
    }

    /**
     * 返回以node为根的二分搜索树的最大值所在的节点
     *
     * @param node
     * @return
     */
    private Node maximum(Node node) {
        if (node.right == null) {
            return node;
        }
        return maximum(node.right);
    }

    public K maximun() {
        if (root == null) {
            return null;
        }
        return maximum(root).key;
    }

    // 删除掉以node为根的二分搜索树的最小节点
    // 返回删除节点后的新的二分搜索树的根
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            size--;
            node.right = null;
            return rightNode;
        }
        node.left = removeMin(node.left);

        return node;
    }

    @Override
    public V put(K key, V value) {
        root = addRec(root, key, value);
        return null;
    }

    private Node addRec(Node node, K key, V value) {
        if (node == null) {
            size++;
            return new Node(key, value);
        }

        if (node.key.compareTo(key) > 0) {
            node.left = addRec(node.left, key, value);
        } else if (node.key.compareTo(key) < 0) {
            node.right = addRec(node.right, key, value);
        } else {
            node.value = value;
        }
        return node;
    }

    @Override
    public V remove(Object key) {
        return null;
    }

    public V remove(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            root = removeRec(root, key);
            return node.value;
        }
        return null;
    }

    private Node removeRec(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) < 0) {
            node.left = removeRec(node.left, key);
            return node;
        } else if (key.compareTo(node.key) > 0) {
            node.right = removeRec(node.right, key);
            return node;
        } else {
            // 待删除节点左子树为空的情况下
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            // 待删除节点右子树为空的情况下
            if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }

            // 待删除节点左右子树都不为空的情况下

            // 找到比待删除节点大的最小节点,,即待删除节点右子树的最小节点
            // 用这个节点来代替被删除的节点
            Node successor = minimum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.right = node.left = null;

            return successor;
        }

    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        // 先访问根节点
        System.out.print(node.key + "==>");
        // 在访问根节点的左子树
        preOrder(node.left);
        // 在访问根节点的右子树
        preOrder(node.right);
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        midOrder(root);
    }

    private void midOrder(Node root) {
        if (root == null) {
            return;
        }
        midOrder(root.left);
        System.out.print(root.key + "==>");
        midOrder(root.right);
    }

    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.key + "==>");
    }


    @Override
    public void putAll(Map<? extends K, ? extends V> m) {

    }

    @Override
    public void clear() {

    }

    @Override
    public Set<K> keySet() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return null;
    }
}
